﻿using AutoMapper;
using MediatR;
using SmartCity.EventHandle.API.Read.Application.Commands;
using SmartCity.EventHandle.API.Read.DTO;
using SmartCity.EventHandle.Domain;
using SmartCity.EventHandle.Errorcode;
using SmartCity.EventHandle.Infrastructure.Extensions;
using SmartCity.EventHandle.Infrastructure.Interfraces;

namespace SmartCity.EventHandle.API.Read.Application.CommandHandlers
{
    public class InstanceQueryCommandHandler:IRequestHandler<InstanceQueryCommand,ApiResult<InstanceDto>>
    {
        private readonly IBaseRepository<WF_Instance> instanceRep;
        private readonly IMapper mapper;

        public InstanceQueryCommandHandler(IBaseRepository<WF_Instance> instanceRep,IMapper mapper)
        {
            this.instanceRep = instanceRep;
            this.mapper = mapper;
        }
        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">请求</param>
        /// <returns>返回单个实例任务</returns>
        public Task<ApiResult<InstanceDto>> Handle(InstanceQueryCommand request, CancellationToken cancellationToken)
        {
            ApiResult<InstanceDto> result = new ApiResult<InstanceDto>();
            result.code = ApiEnum.SearchSuccess;
            result.message = ApiEnum.SearchSuccess.GetDescription();

            var instances = instanceRep.GetAll().FirstOrDefault(x => x.InstanceId.Equals(request.InstanceId));
            result.data = mapper.Map<InstanceDto>(instances);

            return Task.FromResult(result);
        }
    }
}
